﻿// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// Copyright (c) 2011, Dr. Masroor Ehsan
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of the author nor the
//       names of its contributors may be used to endorse or promote products
//       derived from this software without specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// 
//  $Author: masroore@gmail.com $
//  $Id: Program.cs 41 2014-11-22 10:47:38Z masroore@gmail.com $
//  $Rev: 41 $
// 
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

using System;
using System.IO;
using MaxTrader.BusinessObjects;
using MaxTrader.BusinessObjects.Validation;
using MaxTrader.Caching;
using MaxTrader.Configuration;
using MaxTrader.Core;
using MaxTrader.DAL;
using MaxTrader.DAL.Dao.NHibernate;
using MaxTrader.IDQuotesParser.Dse.Sgml;
using MaxTrader.Injection;
using Ninject;
using Ninject.Extensions.Logging;

namespace test_cli
{
    internal class test
    {
        private readonly ILogger _log;

        public test(ILogger log)
        {
            _log = log;
        }

        public void Say(string msg)
        {
            _log.Info(msg);
        }
    }

    public class TestConfig_123 : BaseConfig<TestConfig_123>
    {
        public string HelloMama { get; set; }
        public int Age { get; set; }
        public bool Dead { get; set; }
    }

    internal class Program
    {
        private static void Main(string[] args)
        {
            var b = StockExchangeInfo.Instance.IsAfterMarketOpen(StockExchange.Dse);
            var c = new Money(100, 840);
            using (var kern = DALKernel.BuildKernel())
            {
                NHibernateHelper.CreateDatabase();

                var prep = kern.Get<PortfolioDao>();
                var shrep = kern.Get<ShareAccountDao>();
                var carep = kern.Get<CashAccountDao>();

                for (var i = 0; i < 10; i++)
                {
                    var pf = new Portfolio
                                 {
                                     PortfolioName = "port_" + i,
                                     Currency = Currency.Usd,
                                     StockExchange = StockExchange.Dse
                                 };
                    var sh = new ShareAccount("share" + i, Currency.Bdt) {Portfolio = pf};
                    pf.ShareAccount = sh;
                    prep.Save(pf);
                    shrep.Save(sh);
                }

                var pfs = prep.FindAll();
                for (var i = 0; i < 40; i++)
                {
                    var ca = new CashAccount("cash" + i, Currency.Eur);
                    var pf = pfs[i/4];
                    ca.AddCapital(100m);
                    ca.Portfolio = pf;
                    pf.AddCashAccount(ca);
                    carep.Save(ca);
                    prep.Update(pf);
                }

                var mktrep = kern.Get<MarketSectorDao>();
                var symrep = kern.Get<StockSymbolDao>();
                StockSymbol lastSymb = null;
                for (var i = 0; i < 5; i++)
                {
                    var sect = new MarketSector
                                   {
                                       Name = "Sector #" + i,
                                       Symbol = "sect_" + i
                                   };
                    mktrep.Save(sect);

                    for (var j = 0; j < 5; j++)
                    {
                        var symb = new StockSymbol("XXX" + i*j, "XXX" + i*j, "XXX" + i*j, "XXX" + i*j, "XXX" + i*j,
                                                   MarketCategory.CategoryA, sect);
                        symrep.Save(symb);
                        lastSymb = symb;
                    }
                }


                var txrepo = kern.Get<TransactionDao>();
                for (var i = 0; i < 5; i++)
                {
                    var tx = new ShareTransaction
                                 {
                                     Symbol = lastSymb,
                                     Date = new TradingDate(2011, 10, i + 1),
                                     Amount = new Money(i*456, Currency.Bdt),
                                     TradeCost = new Money(i*123, Currency.Cad),
                                     Category = (TransactionCategory) i
                                 };
                    txrepo.Save(tx);
                }

                var records = txrepo.FindAll();
                foreach (var transaction in records)
                {
                    Console.WriteLine(transaction.Date + " " + transaction.Amount + "   " + transaction.Amount.Currency);
                }
            }
            return;

            using (var fs = new FileStream("ltp1.html", FileMode.Open))
            {
                var sgml = new DseIntradayQuotesParserSgml();
                sgml.ParseQuotes(fs);

/*
                var hap = new DseIntradayQuotesParserHap();
                hap.ParseQuotes(fs);
                return;


                var py = new DseIntradayQuotesParserIronPython();
                py.ParseQuotes(fs);
*/
            }
            using (var fs = new FileStream("ltp1.html", FileMode.Open))
            {
                var sgml = new DseIntradayQuotesParserSgml();
                sgml.ParseQuotes(fs);
            }

            return;

            var k = new StandardKernel();
            //k.Bind<ILogger>.To
            var tll = k.Get<test>();
            tll.Say("hi there");

            CacheProvider.Put("hello", 10991);
            var val = CacheProvider.Get("hello");
            CacheProvider.Flush();
            val = CacheProvider.Get("hello");

            var t = new TestConfig_123();
            t.Age = 102;
            t.Dead = true;
            t.HelloMama = "hello world";
            t.Write();

            using (var kernel = DALKernel.BuildKernel())
            {
                var sect = new MarketSector {Name = " ", Symbol = " x "};
                var repo = kernel.Get<MarketSectorDao>();
                var foo = kernel.Get<SectValService>();
                foo.Val(sect);

                var cont = new ContractBuilder(null)
                    .Price(0)
                    .Quantity(0)
                    .ReferencePrice(0)
                    .Build();
                foo.Val(cont);
            }
        }
    }
}